import { createApp, h } from 'vue'
import TMessage from '@/components/message/MessageManager.vue'

/**
 * created by yangtong on 2025/5/18 14:08:04
 */
let manager: any = null

function mountManager() {
    if (!manager) {
        const container = document.createElement('div')
        const app = createApp({
            render() {
                return h(TMessage, { ref: 'manager' })
            }
        })
        const vm = app.mount(container)
        document.body.appendChild(container)
        manager = vm.$refs.manager
    }
}

export const Message = {
    success(text: string, duration = 3000) {
        mountManager()
        manager.addMessage(text, 'success', duration)
    },
    error(text: string, duration = 3000) {
        mountManager()
        manager.addMessage(text, 'error', duration)
    },
    info(text: string, duration = 3000) {
        mountManager()
        manager.addMessage(text, 'info', duration)
    },
    warning(text: string, duration = 3000) {
        mountManager()
        manager.addMessage(text, 'warning', duration)
    }
}